package tests.function;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.io.IOException;
import java.nio.file.Watchable;

/**
 * Created with IntelliJ IDEA.
 * Description: 登录功能
 * User: Spider-Man
 * Date: 2025-09-21
 * Time: 15:57
 */
public class LoginFunction extends Utils{
    public static final String url = "http://43.143.179.59:8080/blogin.html";
    public static final String adminUrl = "http://43.143.179.59:8080/admin.html";

    /**
     * 成功登录
     * phoneNumber: 13087936712
     * password: 111111
     */
    public void loginSuc() throws IOException {
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("13087936712");//输入账号
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");//输入密码
        driver.findElement(By.cssSelector("#loginForm > button")).click();//登录
        String expect = driver.getCurrentUrl();//获取当前页面url. 因为我们先是对页面进行元素校验的,这里不需要再进行校验
        assert expect.equals(adminUrl); // 校验当前url是否为管理页面url
        Utils.getScreenShot(Thread.currentThread().getName());
    }
    /**
     * 输入错误的密码
     */
    public void loginFail1() throws IOException {
        driver.get(url);
        driver.findElement(By.cssSelector("#phoneNumber")).sendKeys("13087936712");//输入账号
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");//输入密码
        driver.findElement(By.cssSelector("#loginForm > button")).click();//登录
        Alert alert = driver.switchTo().alert();
        String expect = alert.getText();
        assert expect.equals("登录失败!密码错误") : "弹窗信息错误!!!";
        getScreenShot(Thread.currentThread().getName());
        alert.accept();
    }

    /**
     *输入错误账号或密码错误
     */
    public void loginFail2() throws IOException {
        WebElement element = driver.findElement(By.cssSelector("#phoneNumber"));
        element.clear();
        element.sendKeys("13087936722");//输入账号
        element = driver.findElement(By.cssSelector("#password"));
        element.clear();
        element.sendKeys("111111");//输入密码
        driver.findElement(By.cssSelector("#loginForm > button")).click();//登录
        Alert alert = driver.switchTo().alert();
        String expect = alert.getText();
        assert expect.equals("登录失败!用户信息为空") : "弹窗信息错误!!!";
        getScreenShot(Thread.currentThread().getName());
        alert.accept();
        element = driver.findElement(By.cssSelector("#password"));
        element.clear();
        element.sendKeys("111111");//输入错误密码
        alert = driver.switchTo().alert();
        expect = alert.getText();
        assert expect.equals("登录失败!用户信息为空") : "弹窗信息错误!!!";
        getScreenShot(Thread.currentThread().getName());
        alert.accept();
    }

}
